<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title>
<link href="../style/ebook.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>Time zones</h1>
<p>Support for time zones is enabled by default. Airflow stores datetime information in UTC internally and in the database.
It allows you to run your DAGs with time zone dependent schedules. At the moment Airflow does not convert them to the
end user&#x2019;s time zone in the user interface. There it will always be displayed in UTC. Also templates used in Operators
are not converted. Time zone information is exposed and it is up to the writer of DAG what do with it.</p>
<p>This is handy if your users live in more than one time zone and you want to display datetime information according to
each user&#x2019;s wall clock.</p>
<p>Even if you are running Airflow in only one time zone it is still good practice to store data in UTC in your database
(also before Airflow became time zone aware this was also to recommended or even required setup). The main reason is
Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward
in autumn. If you&#x2019;re working in local time, you&#x2019;re likely to encounter errors twice a year, when the transitions
happen. (The pendulum and pytz documentation discusses these issues in greater detail.) This probably doesn&#x2019;t matter
for a simple DAG, but it&#x2019;s a problem if you are in, for example, financial services where you have end of day
deadlines to meet.</p>
<p>The time zone is set in <cite>airflow.cfg</cite>. By default it is set to utc, but you change it to use the system&#x2019;s settings or
an arbitrary IANA time zone, e.g. <cite>Europe/Amsterdam</cite>. It is dependent on <cite>pendulum</cite>, which is more accurate than <cite>pytz</cite>.
Pendulum is installed when you install Airflow.</p>
<p>Please note that the Web UI currently only runs in UTC.</p>
<div class="section" id="concepts">
<h2 class="sigil_not_in_toc">Concepts</h2>
<div class="section" id="naive-and-aware-datetime-objects">
<h3 class="sigil_not_in_toc">Na&#xEF;ve and aware datetime objects</h3>
<p>Python&#x2019;s datetime.datetime objects have a tzinfo attribute that can be used to store time zone information,
represented as an instance of a subclass of datetime.tzinfo. When this attribute is set and describes an offset,
a datetime object is aware. Otherwise, it&#x2019;s naive.</p>
<p>You can use timezone.is_aware() and timezone.is_naive() to determine whether datetimes are aware or naive.</p>
<p>Because Airflow uses time-zone-aware datetime objects. If your code creates datetime objects they need to be aware too.</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">airflow.utils</span> <span class="k">import</span> <span class="n">timezone</span>

<span class="n">now</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span>
<span class="n">a_date</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2017</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
<div class="section" id="interpretation-of-naive-datetime-objects">
<h3 class="sigil_not_in_toc">Interpretation of naive datetime objects</h3>
<p>Although Airflow operates fully time zone aware, it still accepts naive date time objects for <cite>start_dates</cite>
and <cite>end_dates</cite> in your DAG definitions. This is mostly in order to preserve backwards compatibility. In
case a naive <cite>start_date</cite> or <cite>end_date</cite> is encountered the default time zone is applied. It is applied
in such a way that it is assumed that the naive date time is already in the default time zone. In other
words if you have a default time zone setting of <cite>Europe/Amsterdam</cite> and create a naive datetime <cite>start_date</cite> of
<cite>datetime(2017,1,1)</cite> it is assumed to be a <cite>start_date</cite> of Jan 1, 2017 Amsterdam time.</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">default_args</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span>
    <span class="n">start_date</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
    <span class="n">owner</span><span class="o">=</span><span class="s1">&apos;Airflow&apos;</span>
<span class="p">)</span>

<span class="n">dag</span> <span class="o">=</span> <span class="n">DAG</span><span class="p">(</span><span class="s1">&apos;my_dag&apos;</span><span class="p">,</span> <span class="n">default_args</span><span class="o">=</span><span class="n">default_args</span><span class="p">)</span>
<span class="n">op</span> <span class="o">=</span> <span class="n">DummyOperator</span><span class="p">(</span><span class="n">task_id</span><span class="o">=</span><span class="s1">&apos;dummy&apos;</span><span class="p">,</span> <span class="n">dag</span><span class="o">=</span><span class="n">dag</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">op</span><span class="o">.</span><span class="n">owner</span><span class="p">)</span> <span class="c1"># Airflow</span>
</pre>
</div>
</div>
<p>Unfortunately, during DST transitions, some datetimes don&#x2019;t exist or are ambiguous.
In such situations, pendulum raises an exception. That&#x2019;s why you should always create aware
datetime objects when time zone support is enabled.</p>
<p>In practice, this is rarely an issue. Airflow gives you aware datetime objects in the models and DAGs, and most often,
new datetime objects are created from existing ones through timedelta arithmetic. The only datetime that&#x2019;s often
created in application code is the current time, and timezone.utcnow() automatically does the right thing.</p>
</div>
<div class="section" id="default-time-zone">
<h3 class="sigil_not_in_toc">Default time zone</h3>
<p>The default time zone is the time zone defined by the <cite>default_timezone</cite> setting under <cite>[core]</cite>. If
you just installed Airflow it will be set to <cite>utc</cite>, which is recommended. You can also set it to
<cite>system</cite> or an IANA time zone (e.g.`Europe/Amsterdam`). DAGs are also evaluated on Airflow workers,
it is therefore important to make sure this setting is equal on all Airflow nodes.</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">core</span><span class="p">]</span>
<span class="n">default_timezone</span> <span class="o">=</span> <span class="n">utc</span>
</pre>
</div>
</div>
</div>
</div>
<div class="section" id="time-zone-aware-dags">
<h2 class="sigil_not_in_toc">Time zone aware DAGs</h2>
<p>Creating a time zone aware DAG is quite simple. Just make sure to supply a time zone aware <cite>start_date</cite>. It is
recommended to use <cite>pendulum</cite> for this, but <cite>pytz</cite> (to be installed manually) can also be used for this.</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pendulum</span>

<span class="n">local_tz</span> <span class="o">=</span> <span class="n">pendulum</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s2">&quot;Europe/Amsterdam&quot;</span><span class="p">)</span>

<span class="n">default_args</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span>
    <span class="n">start_date</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">local_tz</span><span class="p">),</span>
    <span class="n">owner</span><span class="o">=</span><span class="s1">&apos;Airflow&apos;</span>
<span class="p">)</span>

<span class="n">dag</span> <span class="o">=</span> <span class="n">DAG</span><span class="p">(</span><span class="s1">&apos;my_tz_dag&apos;</span><span class="p">,</span> <span class="n">default_args</span><span class="o">=</span><span class="n">default_args</span><span class="p">)</span>
<span class="n">op</span> <span class="o">=</span> <span class="n">DummyOperator</span><span class="p">(</span><span class="n">task_id</span><span class="o">=</span><span class="s1">&apos;dummy&apos;</span><span class="p">,</span> <span class="n">dag</span><span class="o">=</span><span class="n">dag</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">dag</span><span class="o">.</span><span class="n">timezone</span><span class="p">)</span> <span class="c1"># &lt;Timezone [Europe/Amsterdam]&gt;</span>
</pre>
</div>
</div>
<div class="section" id="templates">
<h3 class="sigil_not_in_toc">Templates</h3>
<p>Airflow returns time zone aware datetimes in templates, but does not convert them to local time so they remain in UTC.
It is left up to the DAG to handle this.</p>
<div class="code python highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pendulum</span>

<span class="n">local_tz</span> <span class="o">=</span> <span class="n">pendulum</span><span class="o">.</span><span class="n">timezone</span><span class="p">(</span><span class="s2">&quot;Europe/Amsterdam&quot;</span><span class="p">)</span>
<span class="n">local_tz</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="n">execution_date</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
<div class="section" id="cron-schedules">
<h3 class="sigil_not_in_toc">Cron schedules</h3>
<p>In case you set a cron schedule, Airflow assumes you will always want to run at the exact same time. It will
then ignore day light savings time. Thus, if you have a schedule that says
run at end of interval every day at 08:00 GMT+1 it will always run end of interval 08:00 GMT+1,
regardless if day light savings time is in place.</p>
</div>
<div class="section" id="time-deltas">
<h3 class="sigil_not_in_toc">Time deltas</h3>
<p>For schedules with time deltas Airflow assumes you always will want to run with the specified interval. So if you
specify a timedelta(hours=2) you will always want to run to hours later. In this case day light savings time will
be taken into account.</p>
</div>
</div>
</body>
</html>